import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;


public class SplashScreen extends JPanel {

	private final ImageIcon blueBall = new ImageIcon("BlueGamepiece.png");
	private final ImageIcon redBall = new ImageIcon("RedGamepiece.png");
	private final ImageIcon net = new ImageIcon("net.png");
	private final ImageIcon background = new ImageIcon("SplashScreen.png");
	public int bluex = 435, redx = 605, bluey = 85, redy = 120;
	public Timer blueTimer = new Timer(1, new BlueListener());
	public Timer redTimer = new Timer(1, new RedListener());
	private HelpFrame help = new HelpFrame();
	private boolean splash;

	public boolean getSplash() {
		return splash;
	}

	//Constructor which adds mouse and key listeners and focuses on the splashscreen so the listeners will work correctly
	public SplashScreen(boolean splash) {
		this.setBackground(Color.black);
		this.addMouseListener(new BallListener());
		this.setFocusable(true);
		this.addKeyListener(new QuitListener());
		this.splash = splash;
	}

	public void paintComponent(Graphics page){
		super.paintComponent(page);

		//Paints the background picture, the two balls and two of the nets
		//The nets are drawn after the balls so that the ball will seem like it is going in the net
		background.paintIcon(null, page, 0, 0);
		blueBall.paintIcon(null, page, bluex, bluey);
		redBall.paintIcon(null, page, redx, redy);
		net.paintIcon(null, page, 430, 260);
		net.paintIcon(null, page, 602, 260);

		//Writes some instruction in gray text
		page.setColor(new Color(200, 200, 200));
		page.setFont(new Font("Cambria", Font.BOLD, 32));
		page.drawString("Click on the red basketball to begin.", 800, 250);
		page.drawString("Click on the blue basketball for help.", 800, 300);
		page.drawString("Press Q to quit.", 800, 350);

	}


	private class BallListener implements MouseListener {

		public void mouseClicked(MouseEvent event) {
			//If the user clicks on the blue basketball, the help frame will show up
			if (Math.abs(Point.distance(event.getLocationOnScreen().x, event.getLocationOnScreen().y, bluex, bluey)) <= 60) {
				blueTimer.start();
				help = new HelpFrame();
				help.setTitle("User Robot Selection");
			}

			//If the user clicks on the red basketball, the selection frame will be created
			if (Math.abs(Point.distance(event.getLocationOnScreen().x, event.getLocationOnScreen().y, redx, redy)) <= 60) {
				redTimer.start();
			}
		}

		//Unimplemented methods
		public void mouseEntered(MouseEvent arg0) {}
		public void mouseExited(MouseEvent arg0) {}
		public void mousePressed(MouseEvent arg0) {}
		public void mouseReleased(MouseEvent arg0) {}


	}

	//The timer listener that allows the red basketball to be animated
	private class BlueListener implements ActionListener {

		private int time = 0;
		public void actionPerformed(ActionEvent e) {
			//Increments the position of the ball 160 times, redrawing the position each time
			time++;
			bluey++;
			repaint();

			//Stops the timer and sets splash to false
			//Shows the Help Panel
			//Resets the positon of the basketball as well
			if(time >= 180) {
				blueTimer.stop();
				help.setTitle("User Robot Selection");
				help.setVisible(true);
				help.setAlwaysOnTop(true);
				bluey = 85;
				time = 0;
			}

		}

	}

	//The timer listener that allows the red basketball to be animated
	private class RedListener implements ActionListener {

		private int time = 0;
		public void actionPerformed(ActionEvent arg0) {
			//Increments the position of the ball 160 times, redrawing the position each time
			time++;
			redy++;
			repaint();

			//Stops the timer and sets splash to false
			//When getSplash() is called in the driver, the while loop will stop and the selection frame will be created
			//Resets the positon of the basketball as well
			if(time >= 160) {
				redTimer.stop();
				splash = false;
				redy = 120;
				time = 0;
			}
		}
	}


	//Allows the user to quit if Q is pressed
	private class QuitListener implements KeyListener {

		public void keyPressed(KeyEvent event) {

			if (event.getKeyCode() == KeyEvent.VK_Q){
				System.exit(0);
			}
		}

		//Unimplemented methods
		public void keyReleased(KeyEvent arg0) {}
		public void keyTyped(KeyEvent arg0) {}


	}
}